package cn.littlehx.spring_boot_test2_2.api;

import cn.littlehx.spring_boot_test2_2.domain.Student;
import cn.littlehx.spring_boot_test2_2.helper.RUtils;
import cn.littlehx.spring_boot_test2_2.service.StudentService;
import cn.littlehx.spring_boot_test2_2.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/api/login")
public class LoginApiController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/student")
    public R student(HttpServletRequest request, @PathParam("name") String name, @PathParam("password") String password) throws Exception{
        Student student = studentService.findByNameAndPassword(name, password).get(0);
if (student!=null){
    HttpSession session = request.getSession();
    session.setAttribute("userid",student.getId());
    session.setAttribute("username",student.getName());
}
        return RUtils.success(student);
    }
}
